package com.wisdytech.linkdcs.equipLifeCycle.maintain.service.impl;

import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenancePlan;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceTask;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dao.IMaintenancePlanDao;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dao.IMaintenanceTaskDao;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dto.MaintenancePlanInteriDTO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.dto.MaintenanceTaskDTO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IMaintenanceTaskService;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-06-13
 */
@Service("iMaintenanceTaskService")
@Transactional
public class MaintenanceTaskServiceImpl extends BaseService<IMaintenanceTaskDao, MaintenanceTask> implements IMaintenanceTaskService {

	@Resource
	private IMaintenancePlanDao iMaintenancePlanDao;
	@Resource
	private IMaintenanceTaskDao iMaintenanceTaskDao;
	
	@Override
	public PageInfo<MaintenanceTask> listTaskByFilter(MaintenanceTaskDTO taskDTO, Filter filter,String loginName) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("equipCode", taskDTO.getEquipCode());
        queryMap.put("equipName", taskDTO.getEquipName());
        queryMap.put("supplier",taskDTO.getSupplier());
        queryMap.put("modelType", taskDTO.getModelType());
        queryMap.put("maintainUserName", loginName);
        queryMap.put("startTime", taskDTO.getStartTime());
        queryMap.put("endTime", taskDTO.getEndTime());
        List<MaintenanceTask> lists =
                super.beforeList(IMaintenanceTaskDao.class, filter).listTaskByFilter(queryMap);
        PageInfo<MaintenanceTask> page=new PageInfo<>(lists);
        return page;
	}

	@Override
	public void addPlanInteri(MaintenancePlanInteriDTO maintenancePlanInteriDTO, String loginName) {
		//保存临时计划
		MaintenancePlan maintenancePlan = new MaintenancePlan();
		String pkId = BaseUtil.getUUID();
		maintenancePlan.setPkId(pkId);
		maintenancePlan.setDelFlag(CommonConstants.DEL_FLAG);
		maintenancePlan.setCreateUser(loginName);
		maintenancePlan.setCreateDate(new Date());
		maintenancePlan.setMaintainConfirm(maintenancePlanInteriDTO.getMaintainConfirm());
		maintenancePlan.setMaintainUserName(maintenancePlanInteriDTO.getMaintainUserName());
		maintenancePlan.setPlanStatus("0");//默认计划有效
		maintenancePlan.setPlanType(CommonConstants.INTERI_PLAN_TYPE);//临时计划
		maintenancePlan.setRemark(maintenancePlanInteriDTO.getRemark());
		maintenancePlan.setTypeId(maintenancePlanInteriDTO.getTypeId());
		int count = iMaintenancePlanDao.insert(maintenancePlan);
        if (count <= 0) {
            throw new ServiceException("新增失败");
        }
        //同时生成临时任务
 	   MaintenanceTask maintenanceTask = new MaintenanceTask();
 	   maintenanceTask.setPkId(BaseUtil.getUUID());
 	   maintenanceTask.setEquipCode(maintenancePlanInteriDTO.getEquipCode());
 	   maintenanceTask.setDelFlag(CommonConstants.DEL_FLAG);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date0 = null;
		try {
			date0 = df.parse(maintenancePlanInteriDTO.getNextDate());
			maintenanceTask.setNextDate(date0);
		} catch (ParseException e) {
			e.printStackTrace();
		}
 	   maintenanceTask.setPlanId(pkId);
 	   maintenanceTask.setType(CommonConstants.INTERI_TASK);//临时保养任务
 	   maintenanceTask.setTaskStatus(CommonConstants.TASK_NO);//生成还未保养得的保养任务
 	   iMaintenanceTaskDao.insert(maintenanceTask);
        if (count <= 0) {
            throw new ServiceException("新增失败");
        }
		
	}

	@Override
	public PageInfo<MaintenanceTask> listTaskConfirmByFilter(MaintenanceTaskDTO taskDTO, Filter filter,
			String loginName) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("equipCode", taskDTO.getEquipCode());
        queryMap.put("equipName", taskDTO.getEquipName());
        queryMap.put("supplier",taskDTO.getSupplier());
        queryMap.put("modelType", taskDTO.getModelType());
        queryMap.put("maintainUserName", loginName);
        queryMap.put("startTime", taskDTO.getStartTime());
        queryMap.put("endTime", taskDTO.getEndTime());
        List<MaintenanceTask> lists =
                super.beforeList(IMaintenanceTaskDao.class, filter).listTaskConfirmByFilter(queryMap);
        PageInfo<MaintenanceTask> page=new PageInfo<>(lists);
        return page;
	}


}
